<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>汇率查询与计算</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background-color: #f4f4f4;
      margin: 0;
      padding: 0;
    }

    .header {
      background-color: #007bff;
      color: white;
      text-align: center;
      padding: 1em;
    }

    .container {
      padding: 20px;
    }

    .form-container {
      background-color: white;
      padding: 20px;
      border-radius: 5px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }

    .form-group {
      margin-bottom: 15px;
    }

    .form-group label {
      display: block;
      margin-bottom: 5px;
      font-weight: bold;
    }

    .form-group input,
    .form-group select {
      width: 100%;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
    }

    .button {
      background-color: #007bff;
      color: white;
      border: none;
      padding: 10px 15px;
      border-radius: 5px;
      cursor: pointer;
      text-decoration: none;
      display: inline-block;
    }

    .button:hover {
      background-color: #0056b3;
    }

    .back-link {
      display: inline-block;
      margin-top: 20px;
      color: #007bff;
      text-decoration: none;
    }

    .back-link:hover {
      text-decoration: underline;
    }

    .result {
      margin-top: 20px;
      padding: 10px;
      background-color: #e9ecef;
      border-radius: 5px;
    }
  </style>
</head>
<body>
  <div class="header">
    <h1>汇率查询与计算</h1>
  </div>

  <div class="container">
    <div class="form-container">
      <form id="exchange-form">
        <div class="form-group">
          <label for="amount">金额:</label>
          <input type="number" id="amount" name="amount" required>
        </div>
        
        <div class="form-group">
          <label for="from_currency">从:</label>
          <select id="from_currency" name="from_currency" required>
            <!-- 这里可以根据实际需要添加更多的选项 -->
            <option value="USD">美元 (USD)</option>
            <option value="EUR">欧元 (EUR)</option>
            <option value="CNY">人民币 (CNY)</option>
          </select>
        </div>
        
        <div class="form-group">
          <label for="to_currency">到:</label>
          <select id="to_currency" name="to_currency" required>
            <!-- 这里可以根据实际需要添加更多的选项 -->
            <option value="USD">美元 (USD)</option>
            <option value="EUR">欧元 (EUR)</option>
            <option value="CNY">人民币 (CNY)</option>
          </select>
        </div>
        
        <button type="submit" class="button">计算汇率</button>
      </form>
      <a href="/finance" class="back-link">返回</a>

      <div id="result" class="result" style="display: none;">
        <p>汇率结果: <span id="converted_amount"></span></p>
      </div>
    </div>
  </div>

  <script>
    document.getElementById('exchange-form').addEventListener('submit', function(event) {
      event.preventDefault();

      const amount = parseFloat(document.getElementById('amount').value);
      const fromCurrency = document.getElementById('from_currency').value;
      const toCurrency = document.getElementById('to_currency').value;

      // 示例汇率数据，可以用实际的API调用来替换
      const exchangeRates = {
        USD: { EUR: 0.85, CNY: 6.45 },
        EUR: { USD: 1.18, CNY: 7.57 },
        CNY: { USD: 0.15, EUR: 0.13 }
      };

      if (fromCurrency === toCurrency) {
        alert("请选择不同的货币进行转换。");
        return;
      }

      const exchangeRate = exchangeRates[fromCurrency][toCurrency];
      const convertedAmount = (amount * exchangeRate).toFixed(2);

      document.getElementById('converted_amount').textContent = `${amount} ${fromCurrency} = ${convertedAmount} ${toCurrency}`;
      document.getElementById('result').style.display = 'block';
    });
  </script>
</body>
</html>